﻿using System;
using System.Collections.Generic;
using System.Linq;
using eCRAM.Tools;
using eCRAM.WebSite.Models;
using eCRAM.Model;
using eCRAM.Bll;
using System.Text;

namespace eCRAM.WebSite.Services
{
    public static class ControllerService
    {

        public static IEnumerable<CramDailyActivityModels> SetDailyActivities(Guid cramLineId, Dictionary<string, string> cramLineModelRates, int year, int month)
        {

            var d = (cramLineModelRates.Where(b => decimal.Parse(b.Value.Replace(".", ",")) != 0));

            var v = d.Where(b => !b.Key.Contains("sumRatesCel"));

            var dailyActivities = v.ToDictionary(
                     b => int.Parse(b.Key.Substring(
                             b.Key.IndexOf("rateCel") + "rateCel".Length,
                            (b.Key.IndexOf("_") - (b.Key.IndexOf("rateCel") + "rateCel".Length))
                     )), b => decimal.Parse(b.Value.Replace(".", ",")));

            List<CramDailyActivityModels> cramDailyActivities = new List<CramDailyActivityModels>();

            foreach (var dailyActivity in dailyActivities)
            {
                CramDailyActivityModels dailyActivityModel = new CramDailyActivityModels();
                dailyActivityModel.CramLineId = cramLineId;
                dailyActivityModel.Day = new DateTime(year, month, dailyActivity.Key + 1);
                dailyActivityModel.Value = dailyActivity.Value;
                cramDailyActivities.Add(dailyActivityModel);
            }
            return cramDailyActivities;
        }


        public static IEnumerable<Cram_DailyActivity> GetDailyActivities(IEnumerable<CramDailyActivityModels> activitiesModel)
        {
            return activitiesModel.Select(GetDailyActivity);
        }

        public static void SaveActivities(Guid cramLineId, IEnumerable<Cram_DailyActivity> activities)
        {
            CramManager.DeleteDailyActivities(cramLineId);
            CramManager.InsertDailyActivities(activities);
        }


        

        public static Cram_DailyActivity GetDailyActivity(CramDailyActivityModels activityModel)
        {
            return new Cram_DailyActivity
                       {
                           Id = Guid.NewGuid(),
                           CramLineId = activityModel.CramLineId,
                           Day = activityModel.Day,
                           Value = activityModel.Value
                       };
        }

        public static CramLine GetCramLine(CramLineModels cramLineModel)
        {
            return new CramLine
                       {
                           Activity = cramLineModel.Activity,
                           Id = cramLineModel.Id
                       };
        }

        public static CramLineModels GetCramLineModel(CramLine cramLine)
        {
            return new CramLineModels
                       {
                           Activity = cramLine.Activity,
                           Id = cramLine.Id,
                           Rates = CramManager.GetMonthRates(cramLine.Cram, cramLine),
                           Status = cramLine.Status,
                       };
        }

        public static CramModels GetCramModel(Cram cram)
        {
            return new CramModels
                       {
                           Id = cram.Id,
                           UserId = cram.UserId,
                           UserName = cram.User.LastName + " " + cram.User.FirstName,

                           UserRole = cram.User.Role,
                          

                           Status = (int)cram.Status,
                           StatusLabel = ((CramStatus)cram.Status).GetLabel(),

                           Comment = cram.Comment,
                           Month = cram.Month,
                           Year = cram.Year,
                           CramlinesModel = cram.CramLines.Select(GetCramLineModel).ToList()
                       };
        }

        public static PrintCramModel GetPrintCramModel(CramModels cram)
        {
            return new PrintCramModel
                       {
                           Id = cram.Id,
                           UserId = cram.UserId,
                           UserName = cram.UserName,

                          
                           StatusLabel = ((CramStatus) cram.Status).GetLabel(),

                           Comment = cram.Comment,
                           Month = cram.Month,
                           Year = cram.Year,
                           CramlinesModel = cram.CramlinesModel,
                           WorkedDays = cram.WorkedDays,
                           WorkingDays = cram.WorkingDays,
                           VacationDays = cram.VacationDays
            };
        }


       


        

    }
}